package com.example.yuan.controller;

import com.example.yuan.anno.Log;
import com.example.yuan.pojo.Dept;
import com.example.yuan.pojo.Emp;
import com.example.yuan.pojo.Result;
import com.example.yuan.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//日志对象
@Scope("prototype")
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //日志对象
    //private static Logger log = LoggerFactory.getLogger(DeptController.class);

    //请求方式
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)


    //查询部门信息
    @GetMapping
    public Result list(){
//        System.out.println("查询全部部门数据");
        List<Dept> deptList =deptService.list();
        log.info("全部部门数据");
        return Result.success(deptList);
    }


    //删除部门信息
    @Log
    @DeleteMapping("/{id}")
    //路径变量id
    public Result delete(@PathVariable  Integer id){
        log.info("根据id删除部门",id);
        deptService.delete(id);
        return Result.success();
    }

    //增加部门名称
    @Log
    @PostMapping
    //封装对象
    public Result add(@RequestBody  Dept dept){
        log.info("新增部门：{}",dept);

        deptService.add(dept);
        return Result.success();
    }

    @Log
    @PutMapping
     public Result update(@RequestBody Emp emp){
        log.info("修改部门信息：{}",emp);
        deptService.update(emp);
        return Result.success();
     }
}
